package com.forage.exp.controller;

import com.forage.exp.constant.GlobalErrorEnum;
import com.forage.exp.constant.ResponseStatusEnum;
import com.forage.exp.po.GlobalException;
import com.forage.exp.po.Response;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import top.iceclean.logtrace.annotation.EnableLogTrace;
import top.iceclean.logtrace.bean.Logger;

import javax.annotation.security.PermitAll;

/**
 * @author : Ice'Clean
 * @date : 2022-05-02
 */
@EnableLogTrace
@RestController
public class TestController {
    /** 固定写法 */
    Logger logTrace;

    /** 通用测试接口 */
    @PermitAll
    @GetMapping("/test")
    public Object test() {
        logTrace.info("调用了测试接口");
        return new Response()
                .addData("content", "测试成功")
                .setStatus(ResponseStatusEnum.OK);
    }

    /**
     * 异常抛出测试
     * @param type 异常类型
     * @return 模拟正常返回值类型
     */
    @GetMapping("/error/{type}")
    public Response error(@PathVariable("type") Integer type) {
        switch (type){
            case 1:
                throw new GlobalException(GlobalErrorEnum.BUSINESS_EXCEPTION, "身体有问题的业务996");
            case 2:
                throw new GlobalException(GlobalErrorEnum.AUTHENTICATION_EXCEPTION,"胳膊有问题的认证哇哈哈");
            case 3:
                throw new GlobalException(GlobalErrorEnum.AUTHORITY_EXCEPTION,"腿毛有问题的权限911");
            default:
                throw new RuntimeException("脑子有问题的未知233");
        }
    }

    /**
     * 用户权限测试
     * @return 模拟正常返回值类型
     */
    @GetMapping("/hello")
    @PreAuthorize("hasAuthority('Admin')")
    public Response hello(){
        return new Response().setStatus(ResponseStatusEnum.OK)
                .addData("hello","你好");
    }
}
